package com.newgoldcurrency.activities.register;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.provider.Settings;
import android.webkit.JavascriptInterface;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.newgoldcurrency.databinding.ActivityRegisterVerifyBinding;
import com.newgoldcurrency.utils.network.Network;

import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterVerifyActivity extends AppCompatActivity {
    private ActivityRegisterVerifyBinding binding;
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityRegisterVerifyBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setResult(1);
        binding.registerVerifyWeb.clearCache(true);
        binding.registerVerifyWeb.getSettings().setJavaScriptEnabled(true);
        binding.registerVerifyWeb.setWebViewClient(new WebViewClient());
        VerifyListener vv=new VerifyListener(Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID));
        binding.registerVerifyWeb.addJavascriptInterface(vv, "VerifyListener");

        HashMap<String, String> para=new HashMap<>();
        Network.post(para, "user/getValidateUrl", call -> call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                runOnUiThread(() -> failed());
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        JsonObject jo = JsonParser.parseString(Objects.requireNonNull(response.body()).string()).getAsJsonObject();
                        binding.registerVerifyWeb.loadUrl(jo.get("data").getAsString());
                    } catch (IOException e) {
                        e.printStackTrace();
                        failed();
                    }
                });
            }
        }));
    }
    private void failed() {
        binding.registerVerifyWeb.loadUrl("http://google.goldtoken.info/validate.html");
    }
    public void verifySuccess(){
        setResult(3);
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding=null;
    }
    private class VerifyListener  {
        public String deviceId;
        public VerifyListener(String deviceId){
            this.deviceId=deviceId;
        }
        //不能删
        @JavascriptInterface
        public String getDeviceId() {
            return deviceId;
        }
        @JavascriptInterface
        public void callback(String result) {
            try {
                runOnUiThread(() -> {
                    if (result.equals("")){
                        Toast.makeText(RegisterVerifyActivity.this, "k", Toast.LENGTH_SHORT).show();
                    }else {
                        JsonObject jo = JsonParser.parseString(result).getAsJsonObject();
                        if (jo.get("data").getAsJsonObject().get("isValid").getAsBoolean()){
                            verifySuccess();
                        }else {
                            Toast.makeText(RegisterVerifyActivity.this, "Please click to verify i am not a robot before registering.", Toast.LENGTH_SHORT).show();
                            RegisterVerifyActivity.this.finish();
                        }
                    }

                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}